#!/bin/bash
Deploy_Path=`cd $(dirname $0); pwd -P`

clean_file(){
    rm -rf openstack/*
    rm -f ceph-openstack.tar.gz
}

mkdir_kolla_config(){
    mkdir -p openstack/config/glance
    mkdir -p openstack/config/cinder/{cinder-volume,cinder-backup}
    mkdir -p openstack/config/nova
    mkdir -p openstack/config/gnocchi
}

create_pools(){
    ceph osd pool create volumes 512 512
    ceph osd pool create images 128 128
    ceph osd pool create backups 512 512
    ceph osd pool create vms 128 128
    ceph osd pool create metrics 256 256
}

init_rbd_pools(){
    rbd pool init volumes
    rbd pool init images
    rbd pool init backups
    rbd pool init vms
}

create_keyring(){
    ceph auth get-or-create client.glance mon 'profile rbd, allow r' osd 'profile rbd, allow class-read object_prefix rbd_children, allow rwx pool=images' mgr 'profile rbd pool=images'
    ceph auth get-or-create client.cinder mon 'profile rbd, allow r' osd 'profile rbd, allow class-read object_prefix rbd_children, allow rwx pool=volumes, allow rwx pool=vms, allow rx pool=images' mgr 'profile rbd pool=volumes, profile rbd pool=vms'
    ceph auth get-or-create client.cinder-backup mon 'profile rbd, allow r' osd 'profile rbd, allow class-read object_prefix rbd_children, allow rwx pool=backups' mgr 'profile rbd pool=backups'
    ceph auth get-or-create client.gnocchi mon "allow r" osd "allow rwx pool=metrics"
}

archive_keyring(){
    ceph auth get-or-create client.glance |tee openstack/config/glance/ceph.client.glance.keyring
    ceph auth get-or-create client.cinder | tee openstack/config/cinder/cinder-volume/ceph.client.cinder.keyring
    ceph auth get-or-create client.cinder-backup | tee openstack/config/cinder/cinder-backup/ceph.client.cinder-backup.keyring
    ceph auth get-or-create client.cinder | tee openstack/config/cinder/cinder-backup/ceph.client.cinder.keyring
    ceph auth get-or-create client.cinder | tee openstack/config/nova/ceph.client.cinder.keyring
    ceph auth get-or-create client.gnocchi |tee openstack/config/gnocchi/ceph.client.gnocchi.keyring
}

copy_conf(){
    cp /etc/ceph/ceph.conf openstack/config/glance/ceph.conf
    cp /etc/ceph/ceph.conf openstack/config/cinder/cinder-volume/ceph.conf
    cp /etc/ceph/ceph.conf openstack/config/cinder/cinder-backup/ceph.conf
    cp /etc/ceph/ceph.conf openstack/config/nova/ceph.conf
    cp /etc/ceph/ceph.conf openstack/config/gnocchi/ceph.conf
}

archive_all(){
    tar cvf ceph-openstack.tar.gz openstack
}

create_openstack_pools(){
    clean_file
    mkdir_kolla_config
    create_pools
    init_rbd_pools
    create_keyring
    archive_keyring
    copy_conf
    archive_all
}

create_openstack_pools
